package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.Affiche;
import cn.stylefeng.guns.modular.yingbao.mapper.AfficheMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.AfficheParam;
import cn.stylefeng.guns.modular.yingbao.model.result.AfficheResult;
import  cn.stylefeng.guns.modular.yingbao.service.AfficheService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 公告模块 服务实现类
 * </p>
 *
 * @author zs
 * @since 2020-01-14
 */
@Service
public class AfficheServiceImpl extends ServiceImpl<AfficheMapper, Affiche> implements AfficheService {

    @Override
    public void add(AfficheParam param){
        Affiche entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(AfficheParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(AfficheParam param){
        Affiche oldEntity = getOldEntity(param);
        Affiche newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public AfficheResult findBySpec(AfficheParam param){
        return null;
    }

    @Override
    public List<AfficheResult> findListBySpec(AfficheParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(AfficheParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryAll() {

        List<Affiche> affiches = this.baseMapper.selectList(new QueryWrapper<Affiche>());
        if(affiches.size() > 0){
            StringBuilder stringBuilder = new StringBuilder();
            affiches.forEach(m -> {
                stringBuilder.append(m.getMessage());
            });
            System.out.println(stringBuilder);
            return ResponseData.success(stringBuilder);
        }
        return ResponseData.error(500,"暂无公告");
    }

    private Serializable getKey(AfficheParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Affiche getOldEntity(AfficheParam param) {
        return this.getById(getKey(param));
    }

    private Affiche getEntity(AfficheParam param) {
        Affiche entity = new Affiche();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
